/** 
  *@CopyRight(c)2009 company Co.Ltd. 
  *@All right reserved.
  *@module name: App.Base.SystemManage.SystemRolesPermsMgrWindow
  *@description:
  *
  *@author: lugj
  *@version: 1.0
  *@date: 2009/07/13
  *@last date: 2009/07/14
  *@history:
    1.
    2.
  *@bugs:
    1. 
    2. 
  *
*/ 

Ext.namespace("App.Base.SystemManage");

//SystemRolesPermsMgrWindow Class
App.Base.SystemManage.SystemRolesPermsMgrWindow = function(config){
	this.config = config;
	this.MODULE_ID = App.Base.Common.func.generateGlobalUniqueId(this.config, 'SYSROLEPERMMGRWIN');

	this.queryCmd = App.Base.common.param.OP_CMD_QUERY;
	
	this.permName = '';
	this.roleName = '';

    this.setHandler = function(handler, scope){
        this.handler = handler;
        this.scope = scope;
    };
	this.setHandler(this.config.handler, this.config.scope);

	this.action = App.Base.common.param.BUTTON_ACTION_NONE;

	//
	this.doQueryPerms = function(){
		this.queryCmd = App.Base.common.param.OP_CMD_QUERY;
		this.permName = Ext.getCmp('txtPermName' + this.MODULE_ID).getValue();
		this.permsStore.load({params:{start:0, limit: Ext.getCmp('pgtblPerms' + this.MODULE_ID).pageSize}});
	};
	
	this.permsStore = new Ext.data.Store({
		id: 'permsStore' + this.MODULE_ID,
        proxy: new Ext.data.HttpProxy({url: App.Base.common.param.SYSPERMSMGR_GRID_DATAURL}),
        reader: new Ext.data.JsonReader({
            totalProperty: 'totalProperty',
            root: 'root'
        }, [
            {name: 'permId'},
            {name: 'permName'},
            {name: 'permDesc'},
            {name: 'permCode'},
            {name: 'permParentName'},
            {name: 'sysHoldFlag'},
            {name: 'state'}
        ]),
		listeners: {
			beforeload: function(store, options){
				options.params.queryCmd = encodeURIComponent(this.queryCmd);
				options.params.permName = encodeURIComponent(this.permName);
			},
			scope: this
		}
    });

	this.permsSm = new Ext.grid.CheckboxSelectionModel(); //{handleMouseDown: Ext.emptyFn}

	this.permsGrid = new Ext.grid.GridPanel({
		id: 'permsGrid' + this.MODULE_ID,
    	border: false,
		autoScroll: true,
        stripeRows: true,
        loadMask: {msg: '系统正在加载权限数据，请稍候...'},
        //autoExpandColumn: 'permName' + this.MODULE_ID,
        enableColumnMove: false,
		sm: this.permsSm,
        cm: new Ext.grid.ColumnModel([
			new Ext.grid.RowNumberer(),
			{id: 'permId' + this.MODULE_ID, header: '编码', dataIndex: 'permId', hidden: true},
			{id: 'permName' + this.MODULE_ID, header: '权限名称', dataIndex: 'permName' , width: 156},
			{header: '权限描述', dataIndex: 'permDesc' , width: 89},
			{header: '权限代码', dataIndex: 'permCode' , width: 70, hidden: true},
			{header: '权限类型', dataIndex: 'permParentName' , width: 100},
			{header: '系统创建', dataIndex: 'sysHoldFlag' , width: 32, align: 'center', hidden: true, renderer: function(value){
					if (value == '1')
					{
						return "是";
					}
					else if (value == '0')
					{
						return "否";
					}
					else
					{
						return "未知";
					}
			}},
			{header: '状态', dataIndex: 'state', width: 24, align: 'center', hidden: true, renderer: function(value){
					if (value == '0')
					{
						return "<font color='black'>正常</font>";
					}
					else if (value == '1')
					{
						return "<font color='green'>已冻结</font>";
					}
					else
					{
						return "<font color='green'>未知</font>";
					}
			}},
			this.permsSm
        ]),
        store: this.permsStore,
		tbar: new Ext.Toolbar({
			border: false,
			items : ['-', 
				new Ext.form.TextField({
					id: 'txtPermName' + this.MODULE_ID,
					emptyText: '请输入搜索的权限名称...',
					width: 220,
					enableKeyEvents: true,
					listeners: {
						keydown: function(o, e){
							if (e.getCharCode() == 13)
							{
								this.doQueryPerms();
							}
						},
						scope: this
					}			
			}),
				new Ext.Button({
					//id: 'btnSearch' + this.MODULE_ID,
					text: '<font color="#0000E3">搜索</font>',
					iconCls: 'x-button-systemmanage-search',
					handler: function(){
						this.doQueryPerms();
					},
					scope: this
			}), '-',
				new Ext.Button({
					//id: 'btnReset' + this.MODULE_ID,
					text: '<font color="#0000E3">重置</font>',
					iconCls: 'x-menu-systemmenusmanage-rset',
					handler: function(){
						Ext.getCmp('txtPermName' + this.MODULE_ID).setValue('');
					},
					scope: this
			}), '-']
		}),
		bbar: new Ext.PagingToolbar({
			id: 'pgtblPerms' + this.MODULE_ID,
            pageSize: App.Base.common.param.GRID_PAGE_SIZE_10,
            store: this.permsStore,
            displayInfo: true,
            displayMsg: '共 {2} 条',//'显示第 {0} 条到 {1} 条记录，共 {2} 条',
            emptyMsg: "没有记录",
            plugins: new Ext.ux.PageSizePlugin()
        }),
        viewConfig: {
            //forceFit: true,
            enableRowBody: true,
            getRowClass: function(record, rowIndex, p, ds) {
                var cls = 'white-row';
                switch (rowIndex%2) {
                    case 1:
                        cls = 'red-row'
                        break;
                }
                return cls;
            }
        }
    });

	//
	this.doQueryRoles = function(){
		this.queryCmd = App.Base.common.param.OP_CMD_QUERY;
		this.roleName = Ext.getCmp('txtRoleName' + this.MODULE_ID).getValue();
		this.rolesStore.load({params:{start:0, limit: Ext.getCmp('pgtblRoles' + this.MODULE_ID).pageSize}});
	};

    this.rolesStore = new Ext.data.Store({
		id: 'rolesStore' + this.MODULE_ID,
        proxy: new Ext.data.HttpProxy({url: App.Base.common.param.SYSROLESMGR_GRID_DATAURL}),
        reader: new Ext.data.JsonReader({
            totalProperty: 'totalProperty',
            root: 'root'
        }, [
            {name: 'roleId'},
            {name: 'roleName'},
            {name: 'roleDesc'},
            {name: 'roleParentName'},
            {name: 'sysHoldFlag'},
            {name: 'state'}
        ]),
		listeners: {
			beforeload: function(store, options){
				options.params.queryCmd = encodeURIComponent(this.queryCmd);
				options.params.roleName = encodeURIComponent(this.roleName);
			},
			scope: this
		}
    });

	this.rolesSm = new Ext.grid.CheckboxSelectionModel();

	this.rolesGrid = new Ext.grid.GridPanel({
		id: 'rolesGrid' + this.MODULE_ID,
    	border: false,
		autoScroll: true,
        stripeRows: true,
        loadMask: {msg: '系统正在加载角色数据，请稍候...'},
        //autoExpandColumn: 'roleName' + this.MODULE_ID,
        enableColumnMove: false,
		sm: this.rolesSm,
        cm: new Ext.grid.ColumnModel([
			new Ext.grid.RowNumberer(),
			{id: 'roleId' + this.MODULE_ID, header: '编码', dataIndex: 'roleId', hidden: true},
			{id: 'roleName' + this.MODULE_ID, header: '角色名称', dataIndex: 'roleName' , width: 156},
			{header: '角色描述', dataIndex: 'roleDesc' , width: 89},
			{header: '角色类型', dataIndex: 'roleParentName' , width: 100},
			{header: '系统创建', dataIndex: 'sysHoldFlag' , width: 20, align: 'center', hidden: true, renderer: function(value){
					if (value == '1')
					{
						return "是";
					}
					else if (value == '0')
					{
						return "否";
					}
					else
					{
						return "未知";
					}
			}},
			{header: '状态', dataIndex: 'state', width: 14, align: 'center', hidden: true, renderer: function(value){
					if (value == '0')
					{
						return "<font color='black'>正常</font>";
					}
					else if (value == '1')
					{
						return "<font color='green'>已冻结</font>";
					}
					else
					{
						return "<font color='green'>未知</font>";
					}
			}},
			this.rolesSm
        ]),

        store: this.rolesStore,

		tbar: new Ext.Toolbar({
			border: false,

			items : ['-', 
				new Ext.form.TextField({
					id: 'txtRoleName' + this.MODULE_ID,
					emptyText: '请输入搜索的角色名称...',
					width: 220,
					enableKeyEvents: true,
					listeners: {
						keydown: function(o, e){
							if (e.getCharCode() == 13)
							{
								this.doQueryRoles();
							}
						},
						scope: this
					}			
			}),
				new Ext.Button({
					id: 'btnSearch' + this.MODULE_ID,
					text: '<font color="#0000E3">搜索</font>',
					iconCls: 'x-button-systemmanage-search',
					handler: function(){
						this.doQueryRoles();
					},
					scope: this
			}), '-',
				new Ext.Button({
					id: 'btnReset' + this.MODULE_ID,
					text: '<font color="#0000E3">重置</font>',
					iconCls: 'x-menu-systemmenusmanage-rset',
					handler: function(){
						Ext.getCmp('txtRoleName' + this.MODULE_ID).setValue('');
					},
					scope: this
			}), '-']
		}),
		
		bbar: new Ext.PagingToolbar({
			id: 'pgtblRoles' + this.MODULE_ID,
            pageSize: App.Base.common.param.GRID_PAGE_SIZE_10,
            store: this.rolesStore,
            displayInfo: true,
            displayMsg: '共 {2} 条',//'显示第 {0} 条到 {1} 条记录，共 {2} 条',
            emptyMsg: "没有记录",
            plugins: new Ext.ux.PageSizePlugin()
        }),
        viewConfig: {
            //forceFit: true,
            enableRowBody: true,
            getRowClass: function(record, rowIndex, p, ds) {
                var cls = 'white-row';
                switch (rowIndex%2) {
                    case 1:
                        cls = 'red-row'
                        break;
                }
                return cls;
            }
        }
    });

	//
	this.form = new Ext.form.FormPanel({
		border: false,
		autoScroll: true,
		frame: true,
		border: false,
		labelAlign: 'top',
		labelWidth: 128,
		bodyStyle:'padding:5px 5px 5px 0',
		layout: 'border',
		items: [{
			region:'west',
			title: '系统角色(必选)',
			width : 440,
			frame: true,
			border: false,
			autoScroll: true,
			bodyStyle:'padding:5px 5px 5px 0',
			split: true,
			//collapsible: true,
			layout: 'fit',
			items :[this.rolesGrid]		
		}, {
			region:'center',
			title: '系统权限(必选)',
			frame: true,
			border: false,
			autoScroll: true,
			bodyStyle:'padding:5px 5px 5px 0',
			layout: 'fit',
			items: [this.permsGrid]
		}]
	});

	this.win = new Ext.Window({
		//id: 'win' + this.MODULE_ID,
		title: this.config.title,
		iconCls: this.config.iconCls,
		width: 920,
		minWidth: 420,
		height: 420,
		minHeight: 360,
		plain: true,
		maximizable: true,
		collapsible: true,
		constrain: true,
		modal: true,
		layout: 'fit',
		items: [this.form],
		buttonAlign: 'right',
		buttons: [{
			text: '确 定',
			handler: function(){
				if(!this.rolesGrid.selModel.hasSelection() || !this.permsGrid.selModel.hasSelection()) 
				{
					App.Base.Common.func.MESSAGEBOX_SHOW({
						title: App.Base.common.param.MESSAGEBOX_NORMAL_TIPS,
						msg: '请选择角色/权限',
						icon: Ext.MessageBox.ERROR
					});					
					return;
				}

				this.action = App.Base.common.param.BUTTON_ACTION_OK;
				this.win.close();
			},
			scope: this
		},{
			text: '取 消',
			handler: function(){
				this.action = App.Base.common.param.BUTTON_ACTION_CANCEL;
				this.win.close();
			},
			scope: this
		}],
		
		listeners: {
			beforeclose: function(o){
				if (!(this.action == App.Base.common.param.BUTTON_ACTION_OK))
				{
					this.action = App.Base.common.param.BUTTON_ACTION_NONE;
				}
				if (this.handler)
				{
					this.handler.call(this.scope || this, this);
				}
				return true;
			},
			scope: this
		}

	});

	this.rolesStore.load({params:{start:0, limit: Ext.getCmp('pgtblRoles' + this.MODULE_ID).pageSize}});
	this.permsStore.load({params:{start:0, limit: Ext.getCmp('pgtblPerms' + this.MODULE_ID).pageSize}});

	//this.win.doLayout();

	//defined methods.
    if ((typeof App.Base.SystemManage.SystemRolesPermsMgrWindow.SIMULATE_SHARED_METHODS_FLAG) == "undefined")
    {
        App.Base.SystemManage.SystemRolesPermsMgrWindow.SIMULATE_SHARED_METHODS_FLAG = true;   

        App.Base.SystemManage.SystemRolesPermsMgrWindow.prototype.getMainPanel = function(){
            return this.win;  
        };
 
        var __ctor = function(){
        }();
    }
}